package org.jsola.hr.helper;

import org.jsola.common.DateKit;
import org.jsola.common.SafeKit;
import org.jsola.hr.constant.SalaryPlanTypeEnum;
import org.jsola.hr.dto.SalaryPlanAddDTO;
import org.jsola.hr.dto.SalaryPlanFormulaAddDTO;
import org.jsola.hr.dto.SalaryPlanUpdateDTO;
import org.jsola.hr.entity.SalaryPlanDO;
import org.jsola.hr.query.SalaryPlanQuery;
import org.jsola.hr.service.ISalaryPlanService;
import org.jsola.hr.vo.SalaryPlanVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

import static org.jsola.hr.core.AbstractTest.SITE_ID;



/**
 * @author zhr
 */
@Component
public class SalaryPlanTestHelper extends BaseTestHelper {

    @Autowired
    private ISalaryPlanService salaryPlanService;
    @Autowired
    private  SalaryPlanFormulaTestHelper salaryPlanFormulaTestHelper;
    @Autowired
    private  SalaryFormulaConditionTestHelper salaryFormulaConditionTestHelper;

    /**
     * 生成add测试数据
     * @return SalaryPlanAddDTO
     */
    public SalaryPlanAddDTO getSalaryPlanAddDTO(){
        SalaryPlanAddDTO salaryPlanAddDTO = new SalaryPlanAddDTO();
        salaryPlanAddDTO.setName(getRandomValue("varchar",255));
        salaryPlanAddDTO.setType(SalaryPlanTypeEnum.SALARY_BASIC.getValue());
        salaryPlanAddDTO.setBeforeTax(SafeKit.getBoolean(getRandomValue("tinyint",1)));
        salaryPlanAddDTO.setNextMonthClear(SafeKit.getBoolean(getRandomValue("tinyint",1)));
        salaryPlanAddDTO.setSystemProperty(SafeKit.getBoolean(getRandomValue("tinyint",1)));
        salaryPlanAddDTO.setRemark(getRandomValue("varchar",255));
        salaryPlanAddDTO.setEnable(SafeKit.getBoolean(getRandomValue("tinyint",1)));
        salaryPlanAddDTO.setCompanyId(getRandomValue("varchar",32));
        return salaryPlanAddDTO;
    }


    /**
     * 生成update测试数据
     * @return SalaryPlanUpdateDTO
     */
    public SalaryPlanUpdateDTO getSalaryPlanUpdateDTO(SalaryPlanVO salaryPlanVO){
        return salaryPlanService.selectDOById(salaryPlanVO.getId(), SITE_ID).to(SalaryPlanUpdateDTO.class);
    }

    /**
     * 生成查询数据
     * @return SalaryPlanQuery
     */
    public SalaryPlanQuery getSalaryPlanQuery() {
        SalaryPlanQuery salaryPlanQuery = new SalaryPlanQuery();
        List<SalaryPlanDO> salaryPlanDOList = salaryPlanService.list(new SalaryPlanDO(),SITE_ID);
        if (!CollectionUtils.isEmpty(salaryPlanDOList)) {
            SalaryPlanDO salaryPlanDO = salaryPlanDOList.get(0);
            salaryPlanQuery = salaryPlanDO.to(SalaryPlanQuery.class);
            if (salaryPlanDO.getGmtCreate() != null) {
                salaryPlanQuery.setGmtCreateStart(DateKit.getBeforeDay(salaryPlanDO.getGmtCreate(),-1));
                salaryPlanQuery.setGmtCreateEnd(DateKit.getBeforeDay(salaryPlanDO.getGmtCreate(),1));
            }
            if (salaryPlanDO.getGmtModified() != null) {
                salaryPlanQuery.setGmtModifiedStart(DateKit.getBeforeDay(salaryPlanDO.getGmtModified(),-1));
                salaryPlanQuery.setGmtModifiedEnd(DateKit.getBeforeDay(salaryPlanDO.getGmtModified(),1));
            }
        }
        return salaryPlanQuery;
    }


    /**
     * 保存示例
     * @return SalaryPlanVO
     */
    public SalaryPlanVO saveSalaryPlanExample(){
        SalaryPlanAddDTO addDTO = this.getSalaryPlanAddDTO();
        SalaryPlanVO salaryPlanVO = salaryPlanService.save(addDTO,buildTokenUser());
        return salaryPlanVO;
    }

    /**
     * 保存示例
     * @return SalaryPlanVO
     */
    public SalaryPlanVO saveSalaryPlanExampleForBusiness(){
        SalaryPlanAddDTO addDTO = this.getSalaryPlanAddDTO();
        List<SalaryPlanFormulaAddDTO> formulaAddDTOList=salaryPlanFormulaTestHelper.getSalaryPlanFormulaAddDTOList();
        formulaAddDTOList.forEach(f-> f.setSalaryFormulaConditionAddDTOList(salaryFormulaConditionTestHelper.getSalaryFormulaConditionAddDTOList()));
        addDTO.setSalaryPlanFormulaAddDTOList(formulaAddDTOList);
        SalaryPlanVO salaryPlanVO = salaryPlanService.save(addDTO,buildTokenUser());
        return salaryPlanVO;
    }

}

